package com.mingqijia.gassafety.webserver.strategy.impl;

import com.mingqijia.gassafety.db.entity.WarningRecord;
import com.mingqijia.gassafety.db.mapper.WarningRecordMapper;

import com.mingqijia.gassafety.shared.constant.MessagePushTypeEnum;
import com.mingqijia.gassafety.webserver.dto.SendMessageDTO;
import com.mingqijia.gassafety.webserver.strategy.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务超期 策略实现
 * </p>
 *
 * @author mby
 * @since 2024-01-16
 */
@Service
@Slf4j
public class WarnRecordService implements RecordService { // 实现策略


    @Autowired
    WarningRecordMapper warningRecordMapper;

    /**
     * 保存服务提醒通知记录
     * @param dto 服务提醒基础数据
     * @param date 时间
     * @param errorMessage 错误信息
     * @param receiptCode 回执编码
     * @param sendResult 发送结果
     * @param status 执行状态
     */
    public void saveWarningRecord(SendMessageDTO dto, Date date, String errorMessage, String receiptCode, Integer sendResult, Integer status) {
        WarningRecord warningRecord = new WarningRecord();

        warningRecord.setStartTime(date);
        //warningRecord.setEndTime(date);
        warningRecord.setWarningStartTime(date);
        warningRecord.setNotifyFrom(0);
        warningRecord.setNotifyObjInfo(new StringBuilder("").append(dto.getCustName()).append("(").append(dto.getSubsCode()).append(")").toString());
        warningRecord.setNotifyObjType(dto.getNotifyObjType());
        warningRecord.setPushPhone(dto.getPhone());
        warningRecord.setConsumerId(String.valueOf(dto.getConsumerId()));
        warningRecord.setTriggerRuleId(dto.getStrategyId().toString());
        warningRecord.setSupIMEI(dto.getSuperImei());
        warningRecord.setIMEI(dto.getImei());
        warningRecord.setStatus(0);  // 默认执行中
        warningRecord.setTaskIdentificationCode(dto.getSnowflakeId().toString());
        warningRecord.setConcentration(dto.getValue().toString());
        warningRecord.setType(dto.getType());
        warningRecord.setSendType(dto.getPushType());
        // 短信和本地消息对调
        if (warningRecord.getSendType() == MessagePushTypeEnum.SMS.getGasWarnCode()) {
            warningRecord.setSendType(MessagePushTypeEnum.LOCAL.getGasWarnCode());
        } else if (warningRecord.getSendType() == MessagePushTypeEnum.LOCAL.getGasWarnCode()) {
            warningRecord.setSendType(MessagePushTypeEnum.SMS.getGasWarnCode());
        }
        warningRecord.setObjLevel(dto.getLevel() == null ? 0 : Integer.parseInt(dto.getLevel()));
        warningRecord.setSendResult(sendResult);
        warningRecord.setContent(dto.getContent());
//        warningRecord.setIsDisabled();
        warningRecord.setRemark(!StringUtils.isEmpty(errorMessage) ? errorMessage : "发送成功");
        warningRecord.setReceiptCode(receiptCode);
        warningRecord.setSpId(dto.getSpId());

        warningRecordMapper.insert(warningRecord);
    }

}
